package com.tjbklx1.thread.chap02.demo02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class TestArrayList {

	public static void main(String[] args) {
		// test01();

		// test02();
		
		test03();
	}
	
	// copyOnWrite
	private static void test03() {
		List<Integer> list = new CopyOnWriteArrayList<Integer>();
		new WriteThread(list).start();
		new ReadThread(list).start();
	}

	// 解决方法 同步list
	private static void test02() {
		List<Integer> list = Collections
				.synchronizedList(new ArrayList<Integer>());
		new WriteThread(list).start();
		new ReadThread(list).start();
	}

	private static void test01() {
		List<Integer> list = new ArrayList<Integer>();
		new WriteThread(list).start();
		new ReadThread(list).start();
		// java.util.ConcurrentModificationException
	}
}
